﻿#include "stdafx.h"
#include "OgreFrameListener.h"
#include "PhysXObj.h"
#include "PhysicsManager.h"
#include "Input.h"
#include "Camera.h"
#include "GameCore.h"
#include "..\3rdLib\Ogitor\Ogitors.h"

_NAMESPACE_BEGIN

OGREFrameListener::OGREFrameListener( RenderWindow* win, Camera* cam, bool bufferedKeys /*= false*/,
										   bool bufferedMouse /*= false*/, bool bufferedJoy /*= false */ ) :
mCamera(cam), mTranslateVector(Vector3::ZERO), mCurrentSpeed(0), mWindow(win), mStatsOn(true), mNumScreenShots(0),
mMoveScale(0.0f), mRotScale(0.0f), mTimeUntilNextToggle(0), mFiltering(TFO_BILINEAR),
mAniso(1), mSceneDetailIndex(0), mMoveSpeed(100), mRotateSpeed(36), mDebugOverlay(0),
mInputManager(0), mMouse(0), mKeyboard(0), mJoy(0)
{
	mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");

	LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
	OIS::ParamList pl;
	size_t windowHnd = 0;
	std::ostringstream windowHndStr;

	win->getCustomAttribute("WINDOW", &windowHnd);
	windowHndStr << windowHnd;
	pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

	mInputManager = OIS::InputManager::createInputSystem( pl );

	//创建所有设备(在这里我们只需要获取操控杆的异常，毕竟大部分人都有键盘鼠标) 
	mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, bufferedKeys ));
	mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, bufferedMouse ));
	try 
	{
		mJoy = static_cast<OIS::JoyStick*>(mInputManager->createInputObject( OIS::OISJoyStick, bufferedJoy ));
	}
	catch(...) 
	{
		mJoy = 0;
	}

	//将创建好的输入设备传给全局输入管理器
	InputCenter::CreateInstance();
	InputCenter::Instance()->SetMouseController(mMouse);
	InputCenter::Instance()->SetKeyBoardController(mKeyboard);
	InputCenter::Instance()->SetJoyStickController(mJoy);

	//设置鼠标移动区域
	windowResized(mWindow);

	//注册窗口监听器
	WindowEventUtilities::addWindowEventListener(mWindow, this);

}

void OGREFrameListener::windowResized( RenderWindow* rw )
{
	unsigned int width, height, depth;
	int left, top;
	rw->getMetrics(width, height, depth, left, top);

	const OIS::MouseState &ms = mMouse->getMouseState();
	ms.width = width;
	ms.height = height;
}

void OGREFrameListener::windowClosed( RenderWindow* rw )
{
	//只关闭创建OIS的窗口（主窗口）
	if( rw == mWindow )
	{
		if( mInputManager )
		{
			mInputManager->destroyInputObject( mMouse );
			mInputManager->destroyInputObject( mKeyboard );
			mInputManager->destroyInputObject( mJoy );

			OIS::InputManager::destroyInputSystem(mInputManager);
			mInputManager = 0;
		}
	}
}

OGREFrameListener::~OGREFrameListener()
{
	//释放接口
	WindowEventUtilities::removeWindowEventListener(mWindow, this);
	windowClosed(mWindow);
}

bool OGREFrameListener::frameRenderingQueued( const FrameEvent& evt )
{
	if(mWindow->isClosed())	return false;

	mSpeedLimit = mMoveScale * evt.timeSinceLastFrame;

	//需要独占/更新每一个设备
	mKeyboard->capture();
	mMouse->capture();
	if( mJoy ) mJoy->capture();

	bool buffJ = (mJoy) ? mJoy->buffered() : true;


	Ogitors::OgitorsRoot::getSingletonPtr()->Update(evt.timeSinceLastFrame);

	return true;
}

bool OGREFrameListener::frameEnded( const FrameEvent& evt )
{
	// 游戏内核更新
	GameCore::Instance()->Update();

	return true;
}

_NAMESPACE_END